/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.core.IOpenValidator;
import net.sourceforge.perforce.core.internal.P4TeamProvider;

import org.eclipse.core.resources.IFile;
import org.eclipse.team.core.RepositoryProvider;

import java.util.List;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
abstract class AbstractOpenValidator implements IOpenValidator {
  //----------------------------------------------------------------------------
  // --------- Methods

  /**
   * Returns an array of files that the need to be opened for edit (based on the
   * given files)
   * 
   * @param files the files.
   * @return array of unopened files.
   */
  IFile[] filesToCheckout(IFile[] files) {
    P4TeamProvider provider =
        (P4TeamProvider) RepositoryProvider.getProvider(files[0].getProject());

    List<IFile> list = Lists.newArrayList();
    for (int i = 0; i < files.length; i++) {
      if (provider.hasRemote(files[i])) {
        if (!provider.isCheckedOut(files[i])) {
          list.add(files[i]);
        }
      }
    }

    return list.toArray(new IFile[list.size()]);
  }
}
